﻿using System.Linq;
using System.Text;
using POSSystem.DAL;
using System;
using POSSystem.BLL;

namespace POSSystem.BLL
{
    public class ShortAd : _ShortAd
    {
        public static ShortAd GetSelectedAds(int top)
        {
            try
            {
                ShortAd ad = new ShortAd();
                ad.Where.Selected.Value = true;
                ad.Where.Deleted.Value = false;
                ad.Query.Top = top;
                ad.Query.AddOrderBy(ShortAd.ColumnNames.LastModifiedDate, MyGeneration.dOOdads.WhereParameter.Dir.DESC);
                ad.Query.Load();

                return ad;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ShortAd.cs", "GetSelectedAds", ex.StackTrace);
                throw ex;
            }
        }

        public static ShortAd GetSelectedAds(int top, bool dealerOnly)
        {
            try
            {
                ShortAd ad = new ShortAd();
                ad.Where.Selected.Value = true;
                ad.Where.Deleted.Value = false;
                ad.Where.DealerOnly.Value = dealerOnly;
                ad.Query.Top = top;
                ad.Query.AddOrderBy(ShortAd.ColumnNames.LastModifiedDate, MyGeneration.dOOdads.WhereParameter.Dir.DESC);
                ad.Query.Load();

                return ad;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ShortAd.cs", "GetSelectedAds", ex.StackTrace);
                throw ex;
            }
        }

        public static ShortAd AddNewAds(string content, bool selected, bool dealerOnly)
        {
            try
            {
                ShortAd ad = new ShortAd();
                ad.AddNew();
                ad.Content = content;
                ad.Selected = selected;
                ad.DealerOnly = dealerOnly;
                ad.Deleted = false;
                ad.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                ad.Save();

                return ad;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ShortAd.cs", "AddNewAds", ex.StackTrace);
                throw ex;
            }
        }

        public static ShortAd UpdateAds(int id, string content, bool selected, bool dealerOnly)
        {
            try
            {
                ShortAd ad = new ShortAd();
                ad.LoadByPrimaryKey(id);
                if (ad.RowCount > 0)
                {
                    ad.Content = content;
                    ad.Selected = selected;
                    ad.DealerOnly = dealerOnly;
                    ad.Deleted = false;
                    ad.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                    ad.Save();
                }
                return ad;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ShortAd.cs", "GetSelectedAds", ex.StackTrace);
                throw ex;
            }
        }

        public static void Delete(int id)
        {
            ShortAd ad = new ShortAd();
            ad.LoadByPrimaryKey(id);
            ad.Deleted = true;
            ad.Save();
        }
    }
}
